/**
 *	Component with spin
 * */
import React, { Component } from 'react';
import { Spin } from 'antd';
import { JSLoad, dataLoad } from './LoadIcon';

/**
 *	给组件嵌套 spin
 * ChildComponent接收方法
 * 组件引入方式 (props) => <Com {...props}/>
 * 地址引入方式 ()=> require('@/views/...')
 * */
const spinComponent = ChildComponent =>
  class extends Component {
    constructor(props) {
      super(props);
      this.state = {
        tip: null,
        loadingIcon: JSLoad,
        loading: true
      };
    }

    componentDidMount() {
      this.setState({ loading: false });
    }

    // 显示 loading
    showLoading = (tip = null) => {
      this.setState({ loading: true, loadingIcon: dataLoad, tip });
    };

    // 隐藏 loading
    hideLoading = () => {
      this.setState({ loading: false });
    };

    render() {
      const { loading, loadingIcon, tip } = this.state;
      const Com = ChildComponent().default || ChildComponent;
      return (
        <Spin spinning={loading} indicator={loadingIcon} tip={tip}>
          <Com showLoading={this.showLoading} hideLoading={this.hideLoading} {...this.props} />
        </Spin>
      );
    }
  };

export default spinComponent;
